﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication2
{
    class Snake
    {
        private List<PictureBox> node;
        private PictureBox head;
        
        private int width = 15;
        private int height = 15;
        private Color color = Color.Blue;
        private Point tail;

        private int moveRatioX;
        private int moveRatioY;

        public Snake()
        {
            Point p = new Point(150, 150);

            head = new PictureBox();
            node = new List<PictureBox>();

            head.Width = width;
            head.Height = height;
            head.BackColor = color;
            head.Location = p;

            moveRatioX = 15;
            moveRatioY = 0;

            for(int i = 0; i < 3; i++)
            {
                p.X -= 15;
                
                PictureBox pb = new PictureBox();
                pb.Width = width;
                pb.Height = height;
                pb.BackColor = color;
                pb.Location = p;
                node.Add(pb);
                tail = p;
            }
        }

        public void AddNode(){
            PictureBox pb = new PictureBox();
            pb.Width = width;
            pb.Height = height;
            Point p = new Point(tail.X - 15, tail.Y - 15);
            tail = p;
            pb.Location = p;
            node.Add(pb);
        }

        public void Draw(Form1 form)
        {
            form.Controls.Add(head);

            foreach (PictureBox pic in node)
            {
                form.Controls.Add(pic);
            }
        }

        public void MoveUp()
        {
            moveRatioX = 0;
            moveRatioY = 15;
        }

        public void MoveDown()
        {
            moveRatioX = 0;
            moveRatioY = 15;
        }

        public void MoveLeft()
        {
            moveRatioX = 15;
            moveRatioY = 0;
        }

        public void MoveRight()
        {
            moveRatioX = -15;
            moveRatioY = 0;
        }

        public void Move()
        {
            for (int i = node.Count - 1; i >= 1; i--)
			{
			    node[i].Location = node[i - 1].Location;
			}

            node[0].Location = head.Location;
            Point p = new Point(head.Location.X + moveRatioX, head.Location.Y + moveRatioY);
            head.Location = p;
        }
    }
}
